package com.demo;

public class AccountingSyncClass implements Runnable {
    static int i = 0;

    static StringBuffer sb = new StringBuffer();

    /**
     * 作用于静态方法,锁是当前class对象,也就是
     * AccountingSyncClass类对应的class对象
     */
    public static synchronized void increase() {
        i++;
    }

    /**
     * 非静态,访问时锁不一样不会发生互斥
     */
    public synchronized void increase4Obj() {
        i++;
        System.out.println(Thread.currentThread().getName() + " " + i);
    }

    @Override
    public void run() {
        for (int j = 0; j < 1000; j++) {
//            System.out.println(Thread.currentThread().getName() + " " + i);
//            increase();
            increase4Obj();
        }
    }

    public static void main(String[] args) throws InterruptedException {

        //new新实例
        Thread t1 = new Thread(new AccountingSyncClass());
        //new心事了
        Thread t2 = new Thread(new AccountingSyncClass());
        //启动线程
        t1.start();
        t2.start();

        t1.join();
        t2.join();
        System.out.println(i);
        System.out.println(sb);
        System.out.println(sb.toString());
    }
}
